<template>
  <el-dialog :model-value="true" :title="title" width="50%" @close="emits('close')">
    <el-form :model="queryParams" label-width="160px" ref="formRef" :rules="rules">
      <el-form-item label="套餐类型名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入套餐类型" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="emits('close')">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ref, toRefs, onMounted } from 'vue'

const props = withDefaults(
  defineProps<{
    title?: string
    editInfo?: any
  }>(),
  {
    title: '新增套餐',
    editInfo: () => ({})
  }
)
const { title, editInfo } = toRefs(props)
const emits = defineEmits(['close', 'submit'])
const formRef = ref<any>(null)
const queryParams = ref({
  name: ''
})
const rules = ref({
  name: [{ required: true, message: '请输入套餐类型', trigger: 'blur' }]
})
const handleSubmit = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      emits('submit', { ...queryParams.value, id: editInfo.value.id })
    }
  })
}
const getEditInfo = () => {
  Object.assign(queryParams.value, {
    name: editInfo.value.name || ''
  })
}

onMounted(() => {
  getEditInfo()
})
</script>
